<template>
  <view class="main" >
    <NavBar title="返现奖励" bgColor="transparent" titleStyle="color:#fff" :autoBack="true" />
    <view v-if="isLogin">
    <view class="header">
      <view class="header-count">
        <view class="header-count-item">
          <view class="val">￥<text class="num">{{queryResult.totalAmount}}</text></view>
          <view class="tit">累计奖金池</view>
        </view>
        <view class="header-count-item count-center">
          <view class="val val-center">￥<text class="num">{{queryResult.currentAmount}}</text></view>
          <view class="tit">当前奖金池</view>
        </view>
        <view  class="header-count-item">
          <view class="val">￥<text class="num">{{queryResult.withDrawAmount}}</text></view>
          <view class="tit">累计提现金额</view>
        </view>
      </view>
      <view class="my-count">
        <view class="my-val">我的可提现金额 <text class="num">{{userBonusCount}}</text> 元</view>
        <view class="my-btn" @click="hintDialogShow = true">去提现</view>
      </view>
    </view>
    <view class="tabs">
      <u-tabs
          :list="nvaList"
          :scrollable="false"
          lineWidth="56rpx"
          lingHeight="4rpx"
          lineColor="#3662EC"
          :itemStyle="{
          height: '80rpx'
        }"
          :activeStyle="{
          color: '#3662EC',
          fontWeight: 500,
        }"
          :inactiveStyle="{
          color: '#333',
          fontSize: '26rpx',
        }"
          @click="handleTab"
      ></u-tabs>
    </view>
    <view class="page-content" >
      <template v-if="queryData.status === 1">
        <view class="list list-head" >
          <view class="order-tit">排队中</view>
          <view class="phone-tit">电话</view>
          <view class="withdraw-amount-tit">申请金额</view>
          <view class="create-time">申请时间</view>
        </view>
        <view class="list-body">
          <view class="list" :class="`list-`+item.order" v-for="(item, index) in queryResult.list" :key="index"  >
            <view class="order">
              <text v-if="!iconTop[item.order]">{{item.order}}</text>
              <image v-else class="icon" :src="iconTop[item.order]" mode="aspectFit" />
            </view>
            <view class="phone">{{item.phoneNum}}</view>
            <view class="withdraw-amount">￥{{item.withdrawAmount}}</view>
            <view style="color: #666">{{item.createTime.substr(0,16)}}</view>
          </view>
        </view>
      </template>
      <template v-else>
        <view class="list list-finish list-head " >
          <view class="phone-tit">电话</view>
          <view class="withdraw-amount-tit">申请金额</view>
          <view class="create-time">申请时间</view>
          <view class="create-time">返现时间</view>
        </view>
        <view class="list-body">
          <view class="list list-finish" v-for="(item, index) in queryResult.list" :key="index"  >
            <view class="phone">{{item.phoneNum}}</view>
            <view class="withdraw-amount">￥{{item.withdrawAmount}}</view>
            <view style="color: #666">{{item.createTime.substr(0,16)}}</view>
            <view style="color: #666"><text v-if="item.finishTime">{{item.finishTime.substr(0,16)}}</text></view>
          </view>
        </view>
      </template>

      <Empty v-if="queryResult.isQueryed && !queryResult.list.length" />
    </view>
    </view>
    <TabbarWithdraw />
    <HintDialog type="withdraw" :show="hintDialogShow" @cancel="()=> {hintDialogShow = false}" @success="withdrawSuccess"/>
      
    <Login />
  </view>
</template>

<script setup>
import { reactive, ref, computed, watch } from 'vue'
import { useStore } from 'vuex'
import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
import { userBonusStatisticsApi, userWithdrawListApi, userWithdrawMyStatisticsApi } from '@/api/user'

import TabbarWithdraw from '@/components/tabbar/TabbarWithdraw.vue';
import icon_top1 from '@/static/svg/icon_top1.svg'
import icon_top2 from '@/static/svg/icon_top2.svg'
import icon_top3 from '@/static/svg/icon_top3.svg'
const store = useStore()
const userBonusCount = computed(()=> {
  return store.state.user.userBonusCount
})
const isLogin = computed(()=> {
  return store.state.user.isLogin
})
const iconTop = {
  1: icon_top1,
  2: icon_top2,
  3: icon_top3,
}
const nvaList = ref([
  {
    name: '排队列表',
    value: 1
  },
  {
    name: '已返现记录',
    value: 2
  },
])
const hintDialogShow = ref(false)
const queryResult = reactive({
  isLast: false,
  isQueryed: false,
  totalAmount: 0,
  currentAmount: 0,
  withDrawAmount: 0,
  totalReceivedAmount: 0,
  totalWaitAmount: 0,
  list: []
})
const queryData = reactive({
  page: 1,
  size: 10,
  status: nvaList.value[0].value
})
watch(
  () => isLogin.value,
  (v) => {
    v && init()
  },
);
onLoad(()=> {
  init()
})
onReachBottom(()=> {
  loadNextPage()
})
async function loadNextPage(){
  if(queryResult.isLast) {
    return
  }
  queryData.page += 1;
  getListData()
}
async function init() {
  userBonusStatistics()
  userWithdrawMyStatistics()
  initQueryList()
}
async function initQueryList(){
  queryData.page = 1;
  queryResult.isLast = false
  await getListData()
}
function handleTab(e) {
  queryData.status = e.value
  initQueryList()
}
function userBonusStatistics() {
  userBonusStatisticsApi().then((res)=> {
    queryResult.currentAmount = res.currentAmount
    queryResult.totalAmount = res.totalAmount
    queryResult.withDrawAmount = res.withDrawAmount
  })
}
function userWithdrawMyStatistics() {
  userWithdrawMyStatisticsApi().then((res)=> {
    queryResult.totalReceivedAmount = res.totalReceivedAmount
    queryResult.totalWaitAmount = res.totalWaitAmount
  })
}
async function getListData(){
  if(queryResult.isLast) {
    return
  }
  uni.showLoading()
  const res = await userWithdrawListApi({...queryData}).finally(()=> {
    uni.hideLoading()
    setTimeout(()=>{
      queryResult.isQueryed = true
    })
  })
  if(queryData.page === 1) {
    queryResult.list = [...res.items]
  } else {
    queryResult.list.push(...res.items)
  }
  queryResult.isLast = queryResult.list.length === res.total

}
function withdrawSuccess(){
  hintDialogShow.value = false
  init()
}
function getShowTime(str){
  return str.subs
}
</script>

<style lang='scss' scoped>
.main{
  min-height: 100vh;
  padding-bottom: 100rpx;
  background-image: url('https://oss.cqxzx.com/dc3d65736f049eb7db5449a50981e0ec.png?Expires=2036414887&OSSAccessKeyId=LTAI5t5a98jUcHvqdxwzahgm&Signature=geZBWIS0hxSzPnuIbU8ZzE%2FisgI%3D');
  background-position: top center;
  background-repeat: no-repeat;
  background-size: 100% auto;
  background-color: #F4F6F8;
  .header{
    width: 750rpx;
    height: 314rpx;
    padding-top: 56rpx;

    .header-count{
      display: flex;
      justify-content: space-between;
      padding: 0 32rpx;
      margin-bottom: 32rpx;
    }
    .header-count-item{
      flex: 1;
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      color: #EAF4FF;
      .tit{
        font-weight: 400;
        font-size: 22rpx;
        line-height: 30rpx;
      }
      .val{
        font-weight: 400;
        font-size: 24rpx;
        line-height: 80rpx;
        height: 80rpx;
        margin-bottom: 20rpx;
        font-style: oblique;
        .num{
          font-size: 48rpx;
          font-weight: bold;
        }
      }
    }
    .count-center{
      color: #FFC300;
      .val{
        .num{
          font-size: 80rpx;
        }
      }
    }
    .my-count{
      margin: 0 auto;
      border-radius: 55rpx 55rpx 55rpx 55rpx;
      display: flex;
      height: 72rpx;
      align-items: center;
      justify-content: center;
      font-weight: 400;
      font-size: 24rpx;
      color: #FFFFFF;
      .my-val{
         height: 72rpx;
        padding: 0 32rpx;
        background: rgba(61,146,250, 0.5);
        border-radius: 55rpx 0 0 55rpx; 
        border: 1rpx solid rgba(255, 255, 255, 0.2);
        border-right: none;
        display: flex;
        align-items: center;
        justify-content: center;
        .num{
          color: #FFC300;
          padding: 0 10rpx;
        }
      }
      .my-btn{
        width: 146rpx;
        height: 72rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(105,172,255, 0.4);
        border-radius: 0 55rpx 55rpx 0; 
        border: 1rpx solid rgba(255, 255, 255, 0.2);
      }
    }
  }
  .tabs{
    margin-bottom: 20rpx;
    background: #fff;
  }
  .page-content{
    background: #fff;
    .list{
      display: grid;
      height: 96rpx;
      padding: 0px 32rpx;
      grid-template-columns: 100rpx 180rpx 1fr 230rpx;
      border-bottom: 1px solid #F4F6F8;
      font-weight: 400;
      font-size: 26rpx;
      color: #333333;
      &.list-head{
        color: #666666;
      }
      &.list-1{background: linear-gradient( 90deg, #FBEFBA 0%, #FDFDFB 100%);}
      &.list-2{background: linear-gradient( 90deg, #D6E1E8 0%, #FFFFFF 100%);}
      &.list-3{background: linear-gradient( 90deg, #F6EBDA 0%, #FDFDFD 100%);}
      view{
        display: flex;
        width: 100%;
        height: 96rpx;
        align-items: center;
      }
      .withdraw-amount{
        color: #FA5151;
      }
      .order{
        font-weight: bold;
        justify-content: center;
        .icon{
          width: 40rpx;
          height: 48rpx;
        }
      }
      .order-tit{
        justify-content: center;
      }
      .phone,.phone-tit{
        padding-left: 30rpx;
      }
      .withdraw-amount-tit,.withdraw-amount{
        justify-content: center;
      }
    }
    
    .list-finish{
      grid-template-columns: 150rpx 1fr 210rpx 210rpx;
      font-size: 22rpx;
      .phone,.phone-tit{
        padding-left: 0;
      }
    }
  }

}
</style>
